package org.brownflat.watergate.scm.svn;

import org.brownflat.watergate.scm.DiffOutput;

import java.io.OutputStream;

import static org.brownflat.utils.util.StringUtil.isEmpty;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
public class SubversionDiffOutput extends OutputStream implements DiffOutput {

    private String internalMemory = "";
    private DiffOutput beforeFile;
    private DiffOutput afterFile;

    @Override
    public void write(int b) {
        byte[] bytes = new byte[1];
        bytes[0] = (byte) (b & 0xff);
        internalMemory = internalMemory + new String(bytes);

        if (internalMemory.endsWith("\n")) {
            internalMemory = internalMemory.substring(0, internalMemory.length () - 1);
        }
    }

    @Override
    public String getOutput() {
        return internalMemory;
    }

    public void setBeforeFile(DiffOutput beforeFile) {
        this.beforeFile = beforeFile;
    }

    public void setAfterFile(DiffOutput afterFile) {
        this.afterFile = afterFile;
    }

    public DiffOutput getBeforeFile() {
        return beforeFile;
    }

    public DiffOutput getAfterFile() {
        return afterFile;
    }

    @Override
    public boolean hasOutput() {
        return isEmpty(internalMemory);
    }
}
